﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.ComponentModel;
using System.Security.Cryptography;

namespace mitasoft.Books.Core
{
    public class FileDescriptor : IFileDescriptor, IEqualityComparer<IFileDescriptor>, INotifyPropertyChanged
    {

        #region ctor
        public FileDescriptor(string path)
        {
            this.FilePath = path;
            this.ID = Guid.NewGuid();
        }
        #endregion

        #region properties

        public Guid ID { get; set; }

        public string FilePath
        {
            get;
            private set;
        }
        public string FileName
        {
            get
            {
                Uri fileUri = new Uri(FilePath);
                return fileUri.GetFilename();
            }
        }

        private long fileSize;
        public long FileSize
        {
            get
            {
                if (fileSize == 0)
                {
                    using (StreamReader reader = new StreamReader(FilePath))
                    {
                        fileSize = reader.BaseStream.Length;
                    }
                }

                return fileSize;
            }
        }

        private decimal fileSizeMB;
        public decimal FileSizeMB
        {
            get
            {
                if (fileSizeMB == 0)
                {
                    if (FileSize > 0)
                        fileSizeMB = Math.Round(FileSize / (decimal)1048576, 2);
                }

                return fileSizeMB;
            }
        }

        private string category;
        public string Category
        {
            get
            {
                return category;
            }
            set
            {
                category = value;

                if (PropertyChanged != null && !string.IsNullOrEmpty(value))
                    PropertyChanged(this, new PropertyChangedEventArgs("Category"));
            }
        }

        private string fileHash;
        public string FileHash
        {
            get
            {
                if (string.IsNullOrEmpty(fileHash))
                    fileHash = GetMD5HashFromFile(FilePath);

                return fileHash;
            }
        }

        #endregion

        #region object specific

        public override string ToString()
        {
            return FileName;
        }
        public bool Equals(IFileDescriptor x, IFileDescriptor y)
        {
            return x.FilePath.Equals(y.FilePath);
        }

        public int HashCode
        {
            get;
            set;
        }
        public int GetHashCode(IFileDescriptor obj)
        {
            return obj.FilePath.GetHashCode();
        }

        #endregion

        #region private methods

        private string GetMD5HashFromFile(string fileName)
        {
            try
            {
                using (FileStream file = new FileStream(fileName, FileMode.Open))
                {
                    MD5 md5 = new MD5CryptoServiceProvider();
                    byte[] retVal = md5.ComputeHash(file);
                    file.Close();

                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < retVal.Length; i++)
                    {
                        sb.Append(retVal[i].ToString("x2"));
                    }
                    return sb.ToString();
                }
            }
            catch
            {
                return "try again";
            }
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }

}
